import BaiduMap from './mapByBaidu';
import config from '../../config/env';
import baseAxios from '../../config/axios';

let map; // BaiduMap 实例对象.
let vm;
let markerMaps = {}; // 点位地图
let fireMaps = []; // 消防点位

export default {
    initMap(vue, id, clickHandle) {
        vm = vue;
        map = new BaiduMap({
            id: id,
            x: 120.558404,
            y: 27.972177,
            zoom: 12,
            minZoom: 6,
            click: clickHandle
        });

        map.setBoundary(() => {
            // 获取网格.
            this.getAjaxStreetGrid(() => {

                //初始化marker标记点
                for (let index = 1; index <= 4; index++) {
                    this.getAjaxSetPos(index, rst => {
                        this.showMarker(rst);
                    });
                }

                //人口密度热力图
                this.getAjaxHotPos();

                // 巡查统计热力图
                this.getAjaxXCTJ();

                //消防点位
                this.getAjaxFireStatus();

            });
        });
    },

    // 显示点位  1-6
    showMarker(rst) {
        if (rst == undefined) return;
        var bmap = map.getMap();
        var temp = {};
        var obj = rst.data;
        for (var id in obj) {
            var marker = obj[id];
            // console.log(marker.cdata);
            var str = marker.cdata.LATITUDE + '_' + marker.cdata.LONGITUDE;

            temp[str] = (temp[str] || 0) + 1;
            if (temp[str] == 1) bmap.addOverlay(marker);
        }
        // this.addStateData(rst.type);
        vm.$store.dispatch('addStatusCount', rst.type);
    },
    // 标记点按钮切换.
    toggleType(type) {

        this.changeType(type);
    },
    // 显示/隐藏 人口密度
    showRKMDLayer(flag) {
        //map.showHotMap(flag);
        map.toggle_RKMD_HotMap(flag);
    },
    // 风险分析 显示/隐藏
    showFXFXLayer(flag) {
        map.show_grid_label(flag);
    },
    // 显示/隐藏 消防点.
    showFireMarker(flag) {
        var bmap = map.getMap();
        for (const marker of fireMaps) {
            if (flag) bmap.addOverlay(marker);
            else bmap.removeOverlay(marker);
        }

        map.toggle_FXFX_HotMap(flag);
    },
    //显示/隐藏 巡查点
    showXCTJLayer(flag) {
        map.toggle_XCTJ_HotMap(flag);
    },
    changeType(type) {
        this.clearMarker();
        if (type == null) return;

        // 清空数量.
        vm.$store.dispatch('clearStatusCount');

        if (type == 0) {
            for (var name in markerMaps) {
                var rst = markerMaps[name];
                this.showMarker(rst);
            }
        } else {

            let rst = markerMaps[type];
            this.showMarker(rst);
        }
    },
    //清除所有地图上marker点.
    clearMarker() {
        let bmap = map.getMap();
        let markers = bmap.getOverlays();
        for (let i = 0; i < markers.length; i++) {
            const item = markers[i];
            if (item.cdata && item.cdata.STATUS !== undefined) {
                bmap.removeOverlay(item);
            }
        }
    },
    // 清除 热力图.
    clearFix() {
        this.showRKMDLayer(false);
        this.showXCTJLayer(false);
        this.showFireMarker(false);
        this.showFXFXLayer(false);
    },
    //街道网格获取
    getAjaxStreetGrid(callback) {
        var url = config.url + 'pubinfo-restful/tbHedaMapDetall/queryAreaGrid.rf';
        //console.log(url);
        baseAxios.get(url).then(response => {
            if (response.data.code === 0) {
                var data = response.data.data;
                map.createGrid(data); //画网格.
            }
            if (typeof callback === 'function') callback();
        });
    },
    //人口密度接口
    getAjaxHotPos(callback) {
        var url = config.url + 'pubinfo-restful/tbHedaMapDetall/queryThermodynamicDiagram.rf';
        //console.log(url);
        baseAxios.get(url).then(response => {
            // console.log(response.data);
            var data = response.data.data;

            //map.hotMapCreate(data);
            map.create_RKMD_HotMap(data);

            if (typeof callback === 'function') {
                callback(hotMap);
            }
        });
    },
    // 巡查统计数据
    getAjaxXCTJ(callback) {
        var url = config.url + 'pubinfo-restful/tbHedaMapDetall/queryInspectMapDetail.rf';
        //console.log(url);
        baseAxios.get(url).then(function (response) {
            if (response.data.code === 0) {
                var data = response.data.data;
                map.create_XCTJ_HotMap(data);
            }
            if (typeof callback === 'function') callback(data);
        });
    },
    //消防点位.
    getAjaxFireStatus(callback) {
        // var self = this;
        var url = config.url + 'pubinfo-restful/tbHedaMapDetall/queryFireDimStation.rf';
        //console.log(url);
        baseAxios.get(url).then(response => {
            if (response.data.code === 0) {
                var list = response.data.data;
                var url = './static/images/state-other-icon1.png'; //消防点位图片.
                for (let i = 0; i < list.length; i++) {
                    const item = list[i];
                    var marker = map.createMarkerPoint({
                        longitude: item.longitude,
                        latitude: item.latitude,
                        url: url,
                        imageSize: 30,
                        data: item,
                        mouseover: function (...params) {
                            vm.pointMouseOver(...params);
                        },
                        mouseout: function (...params) {
                            vm.pointMouseOut(...params);
                        }
                    });

                    fireMaps.push(marker);
                }
            }
        });
    },
    // 标记点获取  1-6按钮
    getAjaxSetPos(type, callback) {
        var url = config.url + 'pubinfo-restful/tdIotDevice/getCoordinateInfo.rf?deviceBigType=' + type;
        //console.log(url);
        var strId = '507383313,507383316,507383342,507423365,507452601';
        var strTemp = strId.split(',');
        var d = {};
        for (let i = 0; i < strTemp.length; i++) {
            const st = strTemp[i];
            d[st] = i;
        }

        baseAxios.get(url).then(function (response) {
            if (response.data.code == 0) {
                var data = response.data.data;

                var obj = map.createMarkers(data.derviceElectricity, d);

                var list = data.countDerviceElectricity;

                var rst = {
                    type: list,
                    data: obj,
                    id: type
                };
                markerMaps[type] = rst;
                if (typeof callback === 'function') {
                    callback(rst);
                }
                console.log('加载' + type + '完毕', markerMaps[type]);
            } else {
                console.log('加载失败', response.data.message);
            }

        });
    },

}
